package com.ygswglxt.notification.controller;

import com.ygswglxt.common.Result;
import com.ygswglxt.notification.entity.Notification;
import com.ygswglxt.notification.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notification")
public class NotificationController {
    
    @Autowired
    private NotificationService notificationService;
    
    // 创建通知（供其他服务调用）
    @PostMapping("/create")
    public Result<String> createNotification(@RequestBody Map<String, Object> params) {
        try {
            Long userId = Long.parseLong(params.get("userId").toString());
            String type = (String) params.get("type");
            String title = (String) params.get("title");
            String content = (String) params.get("content");
            Long relatedId = params.get("relatedId") != null ? Long.parseLong(params.get("relatedId").toString()) : null;
            String relatedType = (String) params.get("relatedType");
            
            notificationService.createNotification(userId, type, title, content, relatedId, relatedType);
            return Result.success("通知创建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("创建通知失败: " + e.getMessage());
        }
    }
    
    // 获取用户通知列表
    @GetMapping("/list")
    public Result<List<Notification>> getNotifications(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            HttpServletRequest request) {
        try {
            String userIdStr = request.getHeader("userId");
            if (userIdStr == null || userIdStr.isEmpty()) {
                return Result.error(401, "用户未登录");
            }
            Long userId = Long.parseLong(userIdStr);
            return notificationService.getUserNotifications(userId, page, size);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取通知列表失败: " + e.getMessage());
        }
    }
    
    // 获取未读数量
    @GetMapping("/unread-count")
    public Result<Map<String, Object>> getUnreadCount(HttpServletRequest request) {
        try {
            String userIdStr = request.getHeader("userId");
            if (userIdStr == null || userIdStr.isEmpty()) {
                return Result.error(401, "用户未登录");
            }
            Long userId = Long.parseLong(userIdStr);
            return notificationService.getUnreadCount(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取未读数量失败: " + e.getMessage());
        }
    }
    
    // 标记为已读
    @PutMapping("/read/{id}")
    public Result<String> markAsRead(@PathVariable Long id, HttpServletRequest request) {
        try {
            String userIdStr = request.getHeader("userId");
            if (userIdStr == null || userIdStr.isEmpty()) {
                return Result.error(401, "用户未登录");
            }
            Long userId = Long.parseLong(userIdStr);
            return notificationService.markAsRead(id, userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("标记失败: " + e.getMessage());
        }
    }
    
    // 全部标记为已读
    @PutMapping("/read-all")
    public Result<String> markAllAsRead(HttpServletRequest request) {
        try {
            String userIdStr = request.getHeader("userId");
            if (userIdStr == null || userIdStr.isEmpty()) {
                return Result.error(401, "用户未登录");
            }
            Long userId = Long.parseLong(userIdStr);
            return notificationService.markAllAsRead(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("标记失败: " + e.getMessage());
        }
    }
    
    // 删除通知
    @DeleteMapping("/{id}")
    public Result<String> deleteNotification(@PathVariable Long id, HttpServletRequest request) {
        try {
            String userIdStr = request.getHeader("userId");
            if (userIdStr == null || userIdStr.isEmpty()) {
                return Result.error(401, "用户未登录");
            }
            Long userId = Long.parseLong(userIdStr);
            return notificationService.deleteNotification(id, userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败: " + e.getMessage());
        }
    }
}

